<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>微信扫码登录</title>
  <style >
	/*全屏loading 防止 手快二次点击*/
	.fullLoading {
	  position: fixed;
	  z-index: 99995;
	  margin: auto;
	  left: 0;
	  right: 0;
	  top: 0;
	  bottom: 0;
	  opacity: 0.8;
	  background-color: transparent;
	  -webkit-animation: comeIn 0.3s linear;
	  animation: comeIn 0.3s linear;
	}
	
	.fullLoading i,
	.fullLoading img,
	.fullLoading>div {
	  position: fixed;
	  z-index: 99996;
	  font-size: 100px;
	  width: 100px;
	  height: 100px;
	  line-height: 1;
	  display: block;
	  position: fixed;
	  margin: auto;
	  left: 0;
	  right: 0;
	  top: -150px;
	  bottom: 0;
	}
	.fullLoading .catLoad0 {
		opacity: 0.6;
	  -webkit-animation: infiniteR0 1.1s cubic-bezier(.15, .73, .37, 1.2) infinite;
	  animation: infiniteR0 1.1s cubic-bezier(.15, .73, .37, 1.2) infinite;
	  color:#5887ef;
	}
	
	.fullLoading .catLoad1 {
		opacity: 0.8;
	  -webkit-animation: infiniteR1 1.1s cubic-bezier(.15, .73, .37, 1.2) infinite;
	  animation: infiniteR1 1.1s cubic-bezier(.15, .73, .37, 1.2) infinite;
	  color:#2d8cf0;
	}
	
	@keyframes infiniteR1 {
	  0% {
		transform: rotate(0deg);
	  }
	
	  80% {
		transform: rotate(360deg);
	  }
	
	  100% {
		transform: rotate(360deg);
	  }
	
	}
	
	@keyframes infiniteR0 {
	  0% {
		transform: rotate(0deg);
	  }
	
	  80% {
		transform: rotate(-360deg);
	  }
	
	  100% {
		transform: rotate(-360deg);
	  }
	
	}
	
	/*加载文字*/
	.fullLoading>div{
	  font-size: 14px;
	  line-height: 1;
	  height: 150px;
	  padding-top: 280px;
	  text-align: center;
	}
	</style>
</head>
<body>
	<div>
        <div class="fullLoading"></div>
        <div class="fullLoading">
            <img src="static/img/zZ01.png" class="catLoad0"/>
            <img src="static/img/zZ02.png" class="catLoad1"/>
            <div>正在请求...</div>
        </div>
	</div>
	


	<script src="static/js/jquery.min.js"></script>
	<script>
		$(function(){
			window.$app = {
				urlF:'https://www.fuwumao.cn/index',
				/**
				 * url传参  可传中文
				 **/
				getRequest: function(obj) {
					var urlStr = location.search,
						theRequest = '';
					var urlCutA = urlStr.split(obj)[1];
					if (!!urlCutA) {
						var urlCutB = urlCutA.split('=')[1];
						var urlCutC = urlCutB.split('&')[0]
						if (urlCutC) {
							theRequest = urlCutC;
						} else {
							theRequest = urlCutB;
						}
						theRequest = decodeURI(theRequest);
						return theRequest;
					} else {
						return;
					}
				},
				ajax:function(url,obj,callBack){
					$.ajax({
						type:'post',
						url: '/api/'+url,
						data:{
							code:code,
							state:state,
						},
						dataType:'json',
						beforeSend: function(request) {
							var token=window.localStorage.getItem('token');
							if(!!token){//先登录再绑定

								//请求头 设置
								request.setRequestHeader('X-FUWUMAO-TOKEN',JSON.parse(token).token);

							}else{//先绑定再登录
								
							}
							
						},
						success: function(res) {
							window.localStorage.removeItem('token');// 删除旧token

							if(res.code==1){
								callBack instanceof Function && callBack(res.data);
							}else{
								alert(res.msg)
								$app.gotoDo();

							}
						},
						error: function(xhr, type, error) {
							window.localStorage.removeItem('token');// 删除旧token

							alert('微信扫码错误')
							console.log(xhr, type, error);
						}
					});
				},

				//错误处理
				gotoDo:function(text){

					// 未登录=>进来这里，bind_wx_init_url是一定不存在的
					// 是否有本地存储的页面？
					var bind_wx_init_url=window.localStorage.getItem('bind_wx_init_url');

					if(!!bind_wx_init_url){
						// 已登录=》个人中心=》绑微信=》个人中心
						window.localStorage.removeItem('bind_wx_init_url');

						location.replace(JSON.parse(bind_wx_init_url));

					}else{

						// 未登录=》绑微信=》登录页
						location.replace($app.urlF+(text||''));

					}
				},
			};








			var code=$app.getRequest('code');
			var state=$app.getRequest('state');
			

			if(!!code && !!state){
				$app.ajax('wechat/qrCodeLoginCallback',{
					code:code,
					state:state,
				},function(resData){
					// console.log('成功回来 = ',JSON.stringify(resData));
					var unionid=resData.unionid;
					var api_token=resData.api_token;

					
					
					if(!!api_token){// 有api_token 直接登录

						let token={
							time:(new Date().getTime())+(86400*1000*1),
							token:api_token
						};
						
						// 本地存储新 token
						window.localStorage.setItem('token', JSON.stringify(token));

						$app.gotoDo();


					}else{//跳转 手机号注册

						$app.gotoDo('/#/login?unionid='+JSON.stringify(resData));
					}

				});
			}else{
				alert('微信扫码错误');
				$app.gotoDo();
			}

			

			





		});

		
	</script>
</body>
</html>